﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace jQuery.Validation.DataAnnotations
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public sealed class AcceptAttribute : ValidationAttribute
    {
        public const string RegExString = @"\.({0})$";

        public string Extension { get; set; }

        public AcceptAttribute(string extension)
        {
            Extension = extension;
        }

        public override bool IsValid(object value)
        {
            if (value == null)
                return true;

            if (value is string)
            {
                Regex regEx = new Regex(string.Format(RegExString, Extension));
                return regEx.IsMatch(value.ToString());
            }

            return false;
            
        }
    }
}
